import numpy
from PyQt4 import QtCore
from KTrade.KOrder import KOrder
from KTrade.KHolding import KHolding
import math
class KPosition(QtCore.QObject):
	''' maintain the position and order information for strategy'''
	def __init__(self,parent):
		QtCore.QObject.__init__(self,parent)
		self.__stratgey=parent
		self.orderlist=[] # maintain all orders placed (KOrder)
		self.holdings={} # maintain all current holdings(KHolding)
		self.origcash=1000 # initial cash in trader's pocket
		self.curcash=1000

	def PlaceOrder(self,symbol,action,price,quant):
		morder=KOrder(self,symbol,action,price,quant)
		self.orderlist.append(morder)

	def GetAllOrders(self):
		# get all executed orders
		return self.orderlist
	
	def __GetAllHoldingsCost(self):
		# get the cost of all stocks in hand
		cost=0.0
		for each_hold in self.holdings:
			cost+=self.holdings[each_hold].cost()
		return cost
	

	def UpdateEachHoldingValue(self,marketvaluedict):
		# update the market value of each holding in this position
		for each_hold in self.holdings:
			self.holdings[each_hold].UpdateMarketValue(marketvaluedict[each_hold])
		
	def GetCurrentProfit(self):
		
		holdingvalue=0.0
		for each_hold in self.holdings:
			holdingvalue+=self.holdings[each_hold].TotalValue()
			
		curvalue=self.curcash+holdingvalue
		profit=	(curvalue-self.origcash)/self.origcash	

		return profit
		
	def CreateHolding(self,symbol,holdtype,price,quant):
		mholding=KHolding(self,symbol,holdtype,price,quant)
		self.holdings[symbol]=mholding 
		return mholding
	
	def GetHoldingCountAndType(self,symbol):
		if symbol not in self.holdings:
			return [0,None]
		else:
			mholding=self.holdings[symbol]
			return [mholding.count(),mholding.Type()]
		
	def GetMaxSharesCanBuy(self,symbol,shareprice):
		# cacluate the maxium number of shares we can buy with current cash
		maxshare=int(math.floor(self.curcash/shareprice))
		return maxshare
		
	def EnterLong(self,symbol, price,quant=100):
		cost=quant*price
		if self.curcash>cost:
			self.PlaceOrder(symbol,KOrder.Action.BUY,price,quant) #place order
			if symbol in self.holdings:
				mholding=self.holdings[symbol]
				success=mholding.AddLongToHolding(price,quant)#add holding
				if success:	
					self.curcash-=cost	
				return success
			else:
				mholding=self.CreateHolding(symbol,KHolding.HoldType.LONG,price,quant)	
				self.curcash-=cost	
			return True
		else:
			print('insufficient cash in account')
			return False
			
	def ExitLong(self,symbol,price,quant=100):
		if symbol not in self.holdings:
			print('can''t exit long since not holding enough shares now ')
			return False
		else:
			mholding=self.holdings[symbol]
			if quant=='ALL':
				quant=mholding.count()
			self.PlaceOrder(symbol,KOrder.Action.SELL,price,quant) #place order		
			success=mholding.ReduceLongFromHolding(quant)	#reduce holding
			if success:
				self.curcash+=price*quant
				return True
			else:
				print('can''t exit long since not holding enough shares now ')	
				return False
			

	def GetReturn():
		return self.cashvalue*1.0/1000







